/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.adcc.webapp.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import vn.adcc.model.Contract;
import vn.adcc.model.Customer;
import vn.adcc.service.ContractManager;
import vn.adcc.service.CustomerManager;

/**
 *
 * @author ducanh.nguyen
 */
public class ContractForm extends BasePage implements Serializable{
    private ContractManager contractManager;
    private CustomerManager customerManager;
    private Contract contract = new Contract();
    private List<SelectItem> customerList;
    private Integer contractId;
    public ContractForm(){
        
    }
    public String add(){
        contract = new Contract();
        return "contractForm";
    }

    public String edit(){
        contract = contractManager.get(contractId);
        return "contractForm";

    }
    public String getValidateContractCode(){
        String contractIdToCheck = getRequest().getParameter("contractForm:contractCode");
        return contractManager.isExists(contractIdToCheck) ? "contract.contractCodeExists":"contract.contractCodeOK";


        //return String.valueOf(new Random().nextInt());
    }

    public String save(){
         try {
            contractManager.save(contract);

        } catch (Exception e) {
            addError("customer.error.saving");
            log.debug(e);
            return "contractForm";
        }
        return "contract";
    }

    public String delete(){
        return "contract";
    }

    public String cancel(){
        return "contract";
    }
    /**
     * @return the contractManager
     */
    public ContractManager getContractManager() {
        return contractManager;
    }

    /**
     * @param contractManager the contractManager to set
     */
    public void setContractManager(ContractManager contractManager) {
        this.contractManager = contractManager;
    }

    /**
     * @return the contract
     */
    public Contract getContract() {
        return contract;
    }

    /**
     * @param contract the contract to set
     */
    public void setContract(Contract contract) {
        this.contract = contract;
    }

    /**
     * @return the contractId
     */
    public Integer getContractId() {
        return contractId;
    }

    /**
     * @param contractId the contractId to set
     */
    public void setContractId(Integer contractId) {
        this.contractId = contractId;
    }
    public void selectCustomer(){
        
    }

    /**
     * @return the customerList
     */
    public List<SelectItem> getCustomerList() {
        List<Customer> list = customerManager.getCustomers();
        SelectItem customer;
        customerList = new ArrayList<SelectItem>();
        for(Customer c : list){
            customer = new SelectItem();
            customer.setLabel(c.getName());
            customer.setValue(c.getName());
            customerList.add(customer);
        }
        return customerList;
    }

    /**
     * @param customerList the customerList to set
     */
    public void setCustomerList(List<SelectItem> customerList) {
        this.customerList = customerList;
    }

    /**
     * @return the customManager
     */
    public CustomerManager getCustomerManager() {
        return customerManager;
    }

    /**
     * @param customManager the customManager to set
     */
    public void setCustomerManager(CustomerManager customerManager) {
        this.customerManager = customerManager;
    }

}
